package com.wiz.web.controllor;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.reflect.TypeToken;
import com.wiz.web.manager.WxManager;
import com.wiz.web.service.MemberService;
import com.wiz.web.vo.criteria.AccountAmountHistoryCriteria;
import com.wiz.web.vo.criteria.AccountChargeHistoryCriteria;
import com.wiz.web.vo.criteria.AccountPointHistoryCriteria;
import com.wiz.web.vo.criteria.MemberCriteria;

@Controller
@RequestMapping("/member")
public class MemberControllor extends BaseControllor{
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private MemberService memberService;
	@Resource
	private WxManager wxManager;
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String addMember(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			return memberService.addMember(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("addMember error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@ResponseBody
	public String getMemberInfo(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			return memberService.getMemberInfo(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getMemberInfo error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/fuzzy", method = RequestMethod.POST)
	@ResponseBody
	public String getMembersByFuzzyQuery(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			return memberService.getMembersByFuzzyQuery(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getMembersByFuzzyQuery error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/grade", method = RequestMethod.POST)
	@ResponseBody
	public String getGrade(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			return memberService.getGradeByMemberId(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getGrade error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public String updateMember(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			return memberService.updateMemberByMemberId(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("updateMember error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/account", method = RequestMethod.POST)
	@ResponseBody
	public String getMemberAccountHistroyList(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			AccountAmountHistoryCriteria criteria = gson.fromJson(member, new TypeToken<AccountAmountHistoryCriteria>(){}.getType());
			return memberService.getMemberAccountHistroyList(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getMemberAccountHistroyList error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/point", method = RequestMethod.POST)
	@ResponseBody
	public String getMemberPointHistroyList(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			AccountPointHistoryCriteria criteria = gson.fromJson(member, new TypeToken<AccountPointHistoryCriteria>(){}.getType());
			return memberService.getMemberPointHistroyList(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getMemberPointHistroyList error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/charge", method = RequestMethod.POST)
	@ResponseBody
	public String getMemberChargeHistroyList(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			AccountChargeHistoryCriteria criteria = gson.fromJson(member, new TypeToken<AccountChargeHistoryCriteria>(){}.getType());
			return memberService.getMemberChargeHistroyList(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getMemberChargeHistroyList error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/recharge", method = RequestMethod.POST)
	@ResponseBody
	public String chargeAmount(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			AccountChargeHistoryCriteria chargeCriteria = gson.fromJson(member, new TypeToken<AccountChargeHistoryCriteria>(){}.getType());
			log.info(stationId+" will chargeAmount,params="+member);
			return memberService.chargeAmountByClient(chargeCriteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("chargeAmount error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/ischeck", method = RequestMethod.POST)
	@ResponseBody
	public String updateIsMemberPassword(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			return memberService.updateIsMemberPassword(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("updateIsMemberPassword error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/editpw", method = RequestMethod.POST)
	@ResponseBody
	public String editMemberPassword(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			log.info(stationId+" will editMemberPassword,params="+member);
			return memberService.editMemberPassword(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("editMemberPassword error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/resetpw", method = RequestMethod.POST)
	@ResponseBody
	public String resetMemberPassword(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			log.info(stationId+" will resetMemberPassword,params="+member);
			return memberService.resetMemberPassword(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("resetMemberPassword error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/status", method = RequestMethod.POST)
	@ResponseBody
	public String updateMemberStatus(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			log.info(stationId+" will updateMemberStatus,params="+member);
			return memberService.updateMemberStatus(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("updateMemberStatus error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/changecard", method = RequestMethod.POST)
	@ResponseBody
	public String changeCard(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			return memberService.changeCard(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("changeCard error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/bindQRCode", method = RequestMethod.POST)
	@ResponseBody
	public String bindOpenId(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model) throws Exception{
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			criteria.setHqId(hqId);
			return memberService.bindMemberOpenId(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("bindOpenId error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/fuess", method = RequestMethod.POST)
	@ResponseBody
	public String updateFuellingStations(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model) throws Exception{
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			return memberService.updateFuellingStations(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("updateFuellingStations error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/chass", method = RequestMethod.POST)
	@ResponseBody
	public String updateChargeStations(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model) throws Exception{
		String member = null;
		try{
			member = getJsonStrByParam(stationId, params);
			MemberCriteria criteria = gson.fromJson(member, new TypeToken<MemberCriteria>(){}.getType());
			return memberService.updateChargeStations(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("updateChargeStations error in "+stationId+"  params="+member,e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
}
